package com.yunquan.ohana.ui.calendar.event.add

import android.os.Bundle
import androidx.activity.result.ActivityResult
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityEventAddBinding
import com.yunquan.ohana.entity.ScheduleModel
import com.yunquan.ohana.utils.StringUtils
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.Locale

class AddEventActivity : BaseActivity<ActivityEventAddBinding, AddEventViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_event_add
    }

    override fun initVariableId(): Int {
        return BR.addEventViewModel
    }

    override fun initParam() {
        super.initParam()
        Locale.setDefault(Locale("en"))
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.c_F6)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        val bundle = intent.extras
        bundle?.let {
            viewModel!!.mDeviceId = it.getString("deviceId")
            viewModel!!.isUserType.set(it.getBoolean("userType", false))
            val dataJson = it.getString("data")
            if (!StringUtils.isEmpty(dataJson)) {
                viewModel!!.mData = Gson().fromJson(dataJson, ScheduleModel::class.java)
                viewModel!!.title.set(getString(R.string.edit_event))
                viewModel!!.isShowDelete.set(true)
                binding!!.tvOperator.setText(R.string.edit)
                viewModel!!.eventTitle.set(viewModel!!.mData?.title)
                viewModel!!.isAllDay.set(viewModel!!.mData?.allDay == 1)
                val zoneId = ZoneId.systemDefault()
                val startInstant = Instant.ofEpochSecond(viewModel!!.mData!!.startTime!!.toLong())
                val startDateTime = LocalDateTime.ofInstant(startInstant, zoneId)
                viewModel!!.mStartDate = startDateTime.toLocalDate()
                viewModel!!.mStartTime = startDateTime.toLocalTime()
                viewModel!!.startDate.set(startDateTime.format(viewModel!!.mDateFormat))
                viewModel!!.startTime.set(startDateTime.format(viewModel!!.mTimeFormat))
                val endInstant = Instant.ofEpochSecond(viewModel!!.mData!!.endTime!!.toLong())
                val endDateTime = LocalDateTime.ofInstant(endInstant, zoneId)
                viewModel!!.mEndDate = endDateTime.toLocalDate()
                viewModel!!.mEndTime = endDateTime.toLocalTime()
                viewModel!!.endDate.set(endDateTime.format(viewModel!!.mDateFormat))
                viewModel!!.endTime.set(endDateTime.format(viewModel!!.mTimeFormat))
                viewModel!!.isRepeats.set(viewModel!!.mData?.repeats == "1")
                viewModel!!.everyCount.set(viewModel!!.mData?.every.toString())
                val everyTypeIndex = (viewModel!!.mData?.recurrence?.toInt() ?: 1) - 1
                viewModel!!.everyTypeIndex.set(everyTypeIndex)
                if (viewModel!!.mData?.repeatsUntil != null) {
                    val repeatUntilInstant = Instant.ofEpochSecond(viewModel!!.mData!!.repeatsUntil!!.toLong())
                    val repeatsUntil = LocalDateTime.ofInstant(repeatUntilInstant, zoneId).toLocalDate()
                    viewModel!!.mRepeatsUntilDate = repeatsUntil
                    viewModel!!.repeatsUntil.set(repeatsUntil.format(viewModel!!.mDateFormat))
                }
                if (everyTypeIndex == 1) {
                    val weekList = viewModel!!.mData?.repetitiveTime?.split(",")
                    weekList?.forEach { week ->
                        when (week.toInt()) {
                            1 -> viewModel!!.everyWeekSun.set(true)
                            2 -> viewModel!!.everyWeekMon.set(true)
                            3 -> viewModel!!.everyWeekTue.set(true)
                            4 -> viewModel!!.everyWeekWen.set(true)
                            5 -> viewModel!!.everyWeekThu.set(true)
                            6 -> viewModel!!.everyWeekFri.set(true)
                            7 -> viewModel!!.everyWeekSat.set(true)
                        }
                    }
                }
            } else {
                viewModel!!.title.set(getString(R.string.add_event))
                viewModel!!.isShowDelete.set(false)
                binding!!.tvOperator.setText(R.string.add)
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel!!.ui.observe(this) {
            if (it == 0) {
                setResult(1002)
                finish()
            } else if (it == 1) {
                setResult(1003)
                finish()
            }
        }
    }

    override fun onActivityResult(result: ActivityResult) {
        super.onActivityResult(result)
        if (result.resultCode == 1001) {
            viewModel!!.getProfilesList()
        }
    }
}

